package com.yy.service.impl;

import com.yy.context.BaseContext;
import com.yy.dto.ShoppingCartDTO;
import com.yy.entity.Dish;
import com.yy.entity.Setmeal;
import com.yy.entity.ShoppingCart;
import com.yy.mapper.DishMapper;
import com.yy.mapper.SetmealMapper;
import com.yy.mapper.ShoppingCartMapper;
import com.yy.result.Result;
import com.yy.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(dto, shoppingCart);
        ShoppingCart cart = shoppingCartMapper.selectDish(shoppingCart);
        if (cart == null) {
            if (dto.getSetmealId() != null) {
                Setmeal setmeal = setmealMapper.selectById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            } else {
                Dish dish = dishMapper.selectById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insertCart(shoppingCart);
        } else {
            cart.setNumber(cart.getNumber() + 1);
            log.info("Id为：{}",cart.getId());
            log.info("Number为：{}",cart.getNumber());
            shoppingCartMapper.updateShoppingCart(cart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    @Override
    public Result delete(ShoppingCartDTO dto) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(dto, cart);
        cart.setUserId(BaseContext.getCurrentId());
        log.info("传入的值为：{}",cart.toString());
        ShoppingCart shoppingCart = shoppingCartMapper.selectDish(cart);
        if(shoppingCart != null && shoppingCart.getNumber() > 0) {
            if (shoppingCart.getNumber() == 1) {
                shoppingCartMapper.delete(cart);
            } else {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateShoppingCart(shoppingCart);
            }
        }else{
            return Result.error("出错了！请联系管理员!");
        }
        return Result.success();
    }

    @Override
    public void deleteAll() {
        shoppingCartMapper.deleteAll(BaseContext.getCurrentId());
    }
}